/*
Automatic clinic tests using a robot arm.
Copyright (C) 2010  CIC-CPC, CHU Gabriel Montpied
All other trademarks, logos and copyrights are the property
of their respective owners.

This file is part of cpc-painbot.

cpc-painbot is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

cpc-painbot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with cpc-painbot.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "testdetermination.h"

#include <QTest>
#include <QFile>
#include <QVariantList>

#include "seuil/determination.h"
#include "seuil/methode/upanddown.h"
#include "stimulation/stimulateurpourtests.h"
#include "rack.h"

void TestDetermination::initTestCase()
{
    determination_ = new seuil::Determination(this);
    stimulateur_ = new StimulateurPourTests(this);

    determination_->setStimulateur(stimulateur_);
}
void TestDetermination::testUpAndDown_init()
{
    seuil::methode::UpAndDown *methode = new seuil::methode::UpAndDown(this);
    determination_->setMethode(methode);

    QVERIFY(methode->type() == seuil::Methode::UpAndDown);

    QFile rack(":/donnees/upanddown.xml");
    rack.open(QIODevice::ReadOnly | QIODevice::Text);
    methode->setRack(Rack::charger(&rack, methode));
}

void TestDetermination::testUpAndDown_data()
{
    QTest::addColumn< QVariantList >("reponses");
    QTest::addColumn<qreal>("seuil");
    QVariantList reponses;
    qreal g = 0.00981;

    reponses << true << true << false;
    QTest::newRow("cas minimal") << reponses << 1.5 * g;

    reponses.clear();
    reponses << false << false << false << true << true << true << true << false;
    QTest::newRow("cas classique") << reponses << 3.5 * g;

    reponses.clear();
    reponses << true << true << true << true << true << true << true << true;
    QTest::newRow("que des ouis") << reponses << qreal(0);

    reponses.clear();
    reponses << false << false << false << false << false << false << false << false;
    QTest::newRow("que des nons") << reponses << std::numeric_limits<qreal>::max();

    reponses.clear();
    reponses << false << false << false << true << false;
    QTest::newRow("erreur confirmation") << reponses << qreal(-1);
}

void TestDetermination::testUpAndDown()
{

    QFETCH(QVariantList, reponses);
    QFETCH(qreal, seuil);
    seuil::Methode *methode = determination_->methode();
    determination_->preparer();

    while ( !reponses.isEmpty() )
        methode->douleur(reponses.takeFirst().toBool());

    QVERIFY(methode->isTerminee());
    QVERIFY(methode->valeur().isValid());
    QVERIFY(methode->valeur().canConvert(QVariant::Double));
    QCOMPARE(methode->valeur().toDouble(), seuil);
}

void TestDetermination::testWindUp()
{
}

